<?php
class CategoryModel extends TaxonomyModel {
    /**
     * status:1=正常，2 隐藏(前台不输出) , 3 冻结(后台不操作)
     */
    protected $fields=array(
       'id','rgt', 'lft', 'ply', 'name', 'jump', 'keywords','describe','total','alias',
       '_pk'=>'id',
       '_autoInc'=>true
    );

    protected $_auto=array(
        array('name','trim','ALL','function'),
        array('jump','processJump','ALL','callback')
    );

    protected $_validate=array(
        array('name','require','{%name_required}',1),
        array('id','/^[1-9]\d*$/','{%id_not_exist}')
    );

    private function getCategoryVo(){
        $categoryVo=$this->create();
        if(!$categoryVo || !array_key_exists('pid',$_POST)){
            return false;
        }

        $pid=$_POST['pid'];
        //检查父栏目是否存在
        $parentNode=$this->where('id='.$pid)->find();
        if(!$parentNode){
            $this->error=L('dao_no_parent_category');
            return false;
        }

        if(array_key_exists('classify',$_POST)){    //栏目类别是列表的话清除跳转地址
            if($_POST['classify'] === 'list'){
                $categoryVo['jump'] = '';
            }
        }

        //检查栏目名称唯一性
        if(false===$this->checkNodeExistedHierarchy($parentNode,$categoryVo)){
            $this->error=sprintf(L('dao_category_name_used'),$categoryVo['name']);
            return false;
        }
        return array('categoryVo'=>$categoryVo,'parentNode'=>$parentNode);
    }
    /**
     * 添加栏目
     */
    public function addCategory(){
        $data=$this->getCategoryVo();
        if(false===$data)  return false;
        //保存节点
        $insertID = $this->insertNode($data['categoryVo'],$data['parentNode']);   //返回新的主键值
        if(false === $insertID){
            $this->error=L('dao_category_save_fail');
            return false;
        }
        $data['categoryVo']['id'] = $insertID;
        return $data['categoryVo'];
    }

    public function updateCategory(){
        $data=$this->getCategoryVo();
        if(false===$data){
            return false;
        }
        $categoryVo=$data['categoryVo'];
        if(false===$this->where("id={$categoryVo['id']}")->save($categoryVo)){
            $this->error = sprintf(L('module_update_category_fail'),$categoryVo['name']);
            return false;
        }
        return $categoryVo;
    }

    /**
     * 对栏目分类进行处理，如果classify为列表，则要清除jump的数据 _auto处理
     */
    public function processJump(){
        return ('list'===$_POST['classify']) ? '' : trim($_POST['jump']);
    }

    /**
     * 获取传入节点的父节点ID
     * @param <array> $node
     * @return <int> pid 父节点ID
     */
    public function getPid($node){
        $parentNode = $this->where("lft<{$node['lft']} and rgt>{$node['rgt']} and ply={$node['ply']}-1")->find();
        if(false === $parentNode){
            return 0;
        }
        return $parentNode['id'];
    }

    /**
     * 更新栏目的使用计数
     */
    public function updateCategoriesNum($ids,$operator='+'){
        return $this->execute("UPDATE __TABLE__ set total=total{$operator}1 where id in ({$ids})");
    }
}
